﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Model.ModelClasses;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.Faculty")]
    public partial class Faculty : AbstractMappingClass, INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private short _idFaculty;

        private string _facultyName;

        private string _facultyShortName;

        private EntitySet<Student> _Students;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(ChangeAction action);
        partial void OnCreated();
        partial void OnidFacultyChanging(short value);
        partial void OnidFacultyChanged();
        partial void OnfacultyNameChanging(string value);
        partial void OnfacultyNameChanged();
        partial void OnfacultyShortNameChanging(string value);
        partial void OnfacultyShortNameChanged();
        #endregion

        public Faculty()
        {
            this._Students = new EntitySet<Student>(new Action<Student>(this.AttachStudents), new Action<Student>(this.DetachStudents));
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idFaculty", AutoSync = AutoSync.OnInsert, DbType = "SmallInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public short idFaculty
        {
            get
            {
                return this._idFaculty;
            }
            set
            {
                if ((this._idFaculty != value))
                {
                    this.OnidFacultyChanging(value);
                    this.SendPropertyChanging();
                    this._idFaculty = value;
                    this.SendPropertyChanged("idFaculty");
                    this.OnidFacultyChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_facultyName", DbType = "VarChar(100) NOT NULL", CanBeNull = false)]
        public string FacultyName
        {
            get
            {
                return this._facultyName;
            }
            set
            {
                if ((this._facultyName != value))
                {
                    this.OnfacultyNameChanging(value);
                    this.SendPropertyChanging();
                    this._facultyName = value;
                    this.SendPropertyChanged("facultyName");
                    this.OnfacultyNameChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_facultyShortName", DbType = "VarChar(15) NOT NULL", CanBeNull = false)]
        public string FacultyShortName
        {
            get
            {
                return this._facultyShortName;
            }
            set
            {
                if ((this._facultyShortName != value))
                {
                    this.OnfacultyShortNameChanging(value);
                    this.SendPropertyChanging();
                    this._facultyShortName = value;
                    this.SendPropertyChanged("facultyShortName");
                    this.OnfacultyShortNameChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.AssociationAttribute(Name = "Faculty_Student", Storage = "_Students", ThisKey = "idFaculty", OtherKey = "refFaculty")]
        public EntitySet<Student> Students
        {
            get
            {
                return this._Students;
            }
            set
            {
                this._Students.Assign(value);
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void AttachStudents(Student entity)
        {
            this.SendPropertyChanging();
            entity.Faculty = this;
        }

        private void DetachStudents(Student entity)
        {
            this.SendPropertyChanging();
            entity.Faculty = null;
        }

        private void UpdateModelEventHandler(object sender, EventArgs e)
        {
            FacultyName = ((Model.ModelClasses.Faculty)sender).Name;
            FacultyShortName = ((Model.ModelClasses.Faculty)sender).Code;
            UpdateData();
        }

        public override AbstractModel PureModel
        {
            get
            {
                var pureModel = new Model.ModelClasses.Faculty { Code = FacultyShortName, Name = FacultyName };
                pureModel.UpdateModelEvent += UpdateModelEventHandler;
                return pureModel;
            }
            set { throw new NotImplementedException(); }
        }
    }
}